clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
putexcel set $Tables/AppendixA1_June.xlsx, replace	

eststo clear
foreach PORTF in BOOK SIZE INV PROF MOME {
use $Work/portfolio_`PORTF'_qnic, clear

rename ge1Mo geMo1
rename ge1Qr geQr1
rename ge1Yr geYr1
rename ge3Yr geYr3
rename ge5Yr geYr5

foreach var of varlist cum*Mo1 geMo1 {
	replace `var'=12*`var'
}	
foreach var of varlist cum*Qr1 geQr1 {
	replace `var'=4*`var'
}	
foreach var of varlist cum*Yr3 geYr3 {
	replace `var'=`var'/3
}	
foreach var of varlist cum*Yr5 geYr5 {
	replace `var'=`var'/5
}	
tsset portf datem
eststo ret_1: quietly: ivreg2 cumNRetMo1 if portf==6, bw(1 ) robust small
eststo ret_2: quietly: ivreg2 cumNRetQr1 if portf==6, bw(3 ) robust small
eststo ret_3: quietly: ivreg2 cumNRetYr1 if portf==6, bw(13) robust small
eststo ret_4: quietly: ivreg2 cumNRetYr3 if portf==6, bw(37) robust small
eststo ret_5: quietly: ivreg2 cumNRetYr5 if portf==6, bw(61) robust small

generate cumQ=cumQRetMo1
eststo reg_1: quietly: ivreg2 cumNRetMo1 cumQ if portf==6, bw(1 ) robust small
replace  cumQ=cumQRetQr1
eststo reg_2: quietly: ivreg2 cumNRetQr1 cumQ if portf==6, bw(3 ) robust small
replace  cumQ=cumQRetYr1
eststo reg_3: quietly: ivreg2 cumNRetYr1 cumQ if portf==6, bw(13) robust small
replace  cumQ=cumQRetYr3
eststo reg_4: quietly: ivreg2 cumNRetYr3 cumQ if portf==6, bw(37) robust small
replace  cumQ=cumQRetYr5
eststo reg_5: quietly: ivreg2 cumNRetYr5 cumQ if portf==6, bw(61) robust small

if "`PORTF'"=="BOOK" {
esttab ret_* using $Tables/AppendixA1_June_ret.csv,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap ar2 title("`PORTF'") replace
esttab reg_* using $Tables/AppendixA1_June_reg.csv,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap ar2 title("`PORTF'") replace
}
if "`PORTF'"!="BOOK" {
esttab ret_* using $Tables/AppendixA1_June_ret.csv,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap ar2 title("`PORTF'") append`'
esttab reg_* using $Tables/AppendixA1_June_reg.csv,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap ar2 title("`PORTF'") append
}

matrix actu= J(5,1,.)
matrix sprd= J(5,1,.)
matrix kapa= J(5,1,.)
matrix gama= J(5,1,.)
matrix prem= J(5,1,.)
matrix revp= J(5,1,.)
matrix   g = J(5,1,.)
matrix slope1= J(5,1,.)
matrix slope2= J(5,1,.)
matrix BETAR = J(5,1,.)
matrix BETAQ = J(5,1,.)

foreach p of numlist 6 {
local i=1
foreach var in Mo1 Qr1 Yr1 Yr3 Yr5  {
	quietly: regress cumQRet`var' if portf==`p'
	matrix sprd[`i',`p'-5]=_b[_cons]

	quietly: regress cumNRet`var' if portf==`p'
	matrix actu[`i',`p'-5]=_b[_cons]

	quietly: regress ge`var' if portf==`p'
	matrix g[`i',`p'-5]=_b[_cons]
	local i=`i'+1
	}
}
matrix list g 
matrix list sprd 
matrix list actu

foreach p of numlist 6 {
local i=1
foreach var in Mo1 Qr1 Yr1 Yr3 Yr5  {
	quietly: eststo Reg`var': regress  cumNRet`var' ge`var' if portf==`p'
	scalar betaR=_b[ge`var']
	display betaR

	quietly: eststo QReg`var': regress cumQRet`var' ge`var' if portf==`p'
	scalar betaQ=_b[ge`var']
	display betaQ

	matrix slope1[`i',`p'-5]=(betaR-1)/(betaQ-1)
	matrix slope2[`i',`p'-5]=(betaR-betaQ)/(betaQ-1)

	matrix BETAR[`i',`p'-5]=betaR
	matrix BETAQ[`i',`p'-5]=betaQ

	local i=`i'+1
	}
}
esttab Reg*
esttab QReg*

foreach p of numlist 6 {
local i=1
foreach var in RetMo1 RetQr1 RetYr1 RetYr3 RetYr5  {
	quietly: eststo: regress cumN`var' cumQ`var' if portf==`p'
	matrix kapa[`i',`p'-5]=_b[_cons]
	matrix gama[`i',`p'-5]=_b[cumQ`var']
	matrix prem[`i',`p'-5]= kapa[`i',`p'-5]+(gama[`i',`p'-5]-1.000000000000000)*sprd[`i',`p'-5]
	
	matrix revp[`i',`p'-5]= kapa[`i',`p'-5]+(gama[`i',`p'-5]-slope1[`i',`p'-5])*sprd[`i',`p'-5]+slope2[`i',`p'-5]*g[`i',`p'-5]

	local i=`i'+1
	}
}
putexcel set $Tables/AppendixA1_June.xlsx, sheet("`PORTF'") modify
putexcel A2="1Mo"
putexcel A3="3Mo"
putexcel A4="1Yr"
putexcel A5="3Yr"
putexcel A6="5Yr"

putexcel B1="Rev prem"
putexcel B2=matrix(revp)
}
